package com.example.androidlistsorter;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.widget.EditText;
import find.prj.lib.SortedMegaList;
import find.prj.lib.TurboList;
import find.prj.prog.ListTesterConsole;

class SortingAsyncTask extends AsyncTask<String,Integer,String> {

	private Activity context;
	private ProgressDialog progressDialog;
	private String consoleText;
	private File file;
	private EditText edtConsole;
	
	final static int NUMBER_OF_TASKS = 4;

	/**
	 * Constructor creates AsyncTask for sorting files using different List instances
	 *  
	 * @param context - Activity from which ProgressDialog will be displayed
	 * @param randomsFileDir - directory containing randoms.txt and where results will be saved
	 * @param consoleText - current text in console of sorting program
	 */
	public SortingAsyncTask(Activity context, String randomsFileDir, EditText edtConsole) {
		this.context = context;
		this.file = new File (randomsFileDir + "//randoms.txt");
		this.edtConsole = edtConsole;
		this.consoleText = edtConsole.getText().toString();
	}

	@Override
	protected void onPreExecute() {
		progressDialog = new ProgressDialog(context);
		progressDialog.setTitle("Processing...");
		progressDialog.setCanceledOnTouchOutside(false);
		progressDialog.setCancelable(true);
		progressDialog.setIndeterminate(false);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setMax(NUMBER_OF_TASKS);
		progressDialog.setOnCancelListener(new OnCancelListener () {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				cancel(true);
			}
		});
		progressDialog.show();	
	}

	@Override
	protected String doInBackground(String... arg0) {
		long startTime, middleTime, finishTime;
		
		this.publishProgress(0);

		if (this.isCancelled())
			return null;
		startTime = System.currentTimeMillis();
		ArrayList<Integer> arrayList = new ArrayList<Integer>();
		ListTesterConsole.fileNumbersToArray (file, arrayList);
		middleTime = System.currentTimeMillis();
		consoleText += "********** Array size = " + arrayList.size() + " **********" + "\n";
		consoleText += "File loaded: " + (middleTime-startTime) + "\n";
		ListTesterConsole.ArrayListSort(arrayList, false);
		finishTime = System.currentTimeMillis();
		consoleText += "Sorted: "+(finishTime-startTime) + "\n";
		ListTesterConsole.ListToFile(arrayList, arg0[0] + "//arraylist.txt");
		this.publishProgress(1);
		
		if (this.isCancelled())
			return null;
		startTime = System.currentTimeMillis();
		TurboList<Integer> turboList = new TurboList<Integer>();
		ListTesterConsole.fileNumbersToArray (file, turboList);
		middleTime = System.currentTimeMillis();
		consoleText += "File loaded: " + (middleTime-startTime) + "\n";
		ListTesterConsole.TurboListSort(turboList, false);
		finishTime = System.currentTimeMillis();
		consoleText += "Sorted: "+(finishTime-startTime) + "\n";
		ListTesterConsole.ListToFile(arrayList, arg0[0] + "//turbolist.txt");
		this.publishProgress(2);

		if (this.isCancelled())
			return null;
		startTime = System.currentTimeMillis();
		ArrayList<Integer> arrayList2 = new ArrayList<Integer>();
		ListTesterConsole.fileNumbersToArray (file, arrayList2);
		int[] array = new int[arrayList.size()];
		for (int i=0; i < array.length; i++)
			array[i] = arrayList.get(i);
		middleTime = System.currentTimeMillis();
		consoleText += "File loaded: " + (middleTime-startTime) + "\n";
		ListTesterConsole.ArraySort(array, false);
		finishTime = System.currentTimeMillis();
		consoleText += "Sorted: "+(finishTime-startTime) + "\n";
		arrayList2.clear();
		for (Integer value : array)
			arrayList2.add(value);
		ListTesterConsole.ListToFile(arrayList2, arg0[0] + "//array.txt");
		this.publishProgress(3);
		
		if (this.isCancelled())
			return null;
		startTime = System.currentTimeMillis();
		SortedMegaList sortedMegaList = new SortedMegaList();
		ListTesterConsole.fileNumbersToArray (file, sortedMegaList);
		middleTime = System.currentTimeMillis();
		consoleText += "File loaded: " + (middleTime-startTime) + "\n";
		finishTime = System.currentTimeMillis();
		consoleText += "Sorted: "+(finishTime-startTime) + "\n";
		ListTesterConsole.ListToFile(arrayList, arg0[0] + "//sortedmegalist.txt");	
		this.publishProgress(4);

		return consoleText;
	}
	
	@Override
	protected void onProgressUpdate(Integer... values) {
		progressDialog.setProgress(values[0]);
		switch (progressDialog.getProgress()) {
			case 0: progressDialog.setMessage("Sorting ArrayList..."); break;
			case 1: progressDialog.setMessage("Sorting TurboList..."); break;
			case 2: progressDialog.setMessage("Sorting Array..."); break;
			case 3: progressDialog.setMessage("Sorting SortedMegaList..."); break;
			case 4: progressDialog.setMessage("Finished"); break;
		}
	}
	
	@Override
	protected void onCancelled() {
	}
	
	@Override
	protected void onPostExecute(String v) {
		progressDialog.dismiss();
		edtConsole.setText(consoleText);
	}

}